﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using D3D = Microsoft.DirectX.Direct3D;
using Microsoft.DirectX.DirectInput;
using System.Threading;

namespace CubePuzzle
{
    public partial class Game : Form
    {
        private DxAppGame dxgame;
        private DxAppMenu dxmenu;
        private Thread threadGame;
        private Thread threadMenu;
        private IO io;
        private MatrixBuilder matbuild;


        public void renderGame()
        {
            while (true)
            {
                dxgame.Render();
            }
        }

        public void renderMenu()
        {
            while (true)
            {
                dxmenu.Render();
            }
        }

        public void endGame()
        {
            threadGame.Abort();
            threadMenu.Abort();
            dxgame.device.Dispose();
            dxmenu.device.Dispose();
        }

        public void newGame(int index)
        {
            io = new IO(index);
            matbuild = new MatrixBuilder(io.getItems(), io.getDim(), io.getNumberOfItems());
            matbuild.buildMatrix();
            dxgame = new DxAppGame(pbGame.Handle, this, matbuild);
            dxmenu = new DxAppMenu(pbMenu.Handle, this, matbuild);
            threadGame = new Thread(renderGame);
            threadMenu = new Thread(renderMenu);
            threadMenu.Start();
            threadGame.Start();
            cbSelect.Items.Clear();
            for (int i = 0; i < matbuild.K; i++)
            {
                cbSelect.Items.Add((i + 1).ToString());
            }
        }

        public Game()
        {
            InitializeComponent();
            newGame(3);
            this.KeyPreview = true;
            this.KeyPress += new KeyPressEventHandler(Game_KeyPress);
            cbSelect.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        private void bSelect_Click(object sender, EventArgs e)
        {
            String tmp = (String)cbSelect.SelectedItem;
            try
            {
                int tmpSelected = Convert.ToInt32(tmp);
                if (tmpSelected != 0)
                {
                    dxgame.ItemIndex = tmpSelected;
                    dxmenu.State = tmpSelected;
                    cbSelect.Enabled = false;
                }
            }
            catch (NullReferenceException nre)
            {
                MessageBox.Show(nre.Message);
                dxgame.ItemIndex = 0;
            }
        }

        private void bDeselect_Click(object sender, EventArgs e)
        {
            cbSelect.Enabled = true;
            dxgame.ItemIndex = 0;
            dxmenu.State = 0;
        }

        void Game_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 48)
            {
                cbGame.Checked = !cbGame.Checked;
            }

            if (e.KeyChar == 57)
            {
                cbSelect.Enabled = true;
                dxgame.ItemIndex = 0;
                dxmenu.State = 0;
            }

            if (e.KeyChar == 56)
            {
                String tmp = (String)cbSelect.SelectedItem;
                try
                {
                    int tmpSelected = Convert.ToInt32(tmp);
                    if (tmpSelected != 0)
                    {
                        dxgame.ItemIndex = tmpSelected;
                        dxmenu.State = tmpSelected;
                        cbSelect.Enabled = false;
                    }
                }
                catch (NullReferenceException nre)
                {
                    MessageBox.Show(nre.Message);
                    dxgame.ItemIndex = 0;
                }
            }
        }

        private void mThree_Click(object sender, EventArgs e)
        {
            endGame();
            newGame(3);
        }

        private void mFour_Click(object sender, EventArgs e)
        {
            endGame();
            newGame(4);
        }

        private void mFive_Click(object sender, EventArgs e)
        {
            endGame();
            newGame(5);
        }

        private void cbGame_CheckedChanged(object sender, EventArgs e)
        {
            cbSelect.Enabled = !cbSelect.Enabled;
            bDeselect.Enabled = !bDeselect.Enabled;
            bSelect.Enabled = !bSelect.Enabled;
        }
    }
}
